<?
header ("content-type: text/html; charset=utf-8");
$admin_area=1;
include "../includes/common_admin_inc.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<?
$lCharSetString="CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$lVerArray=getMySQLVersion();

$lMajor=$lVerArray[0];
$lSub=$lVerArray[1];
if ($lMajor !=5)
{
	if ($lMajor==4 && $lSub==0)
		$lSkipUTF=1;
	if ($lMajor==3)
		$lSkipUTF=1;
}

if ($lSkipUTF==1)
	$lCharSetString="";


//$set_multilang_opt;

// Find what Multi Language Options to support. If column doesn't exists, create it.
$lLangSupportedArray=split(",",$set_multilang_opt);
$lLangInDatabaseNow=getFields($opt_tbl);
foreach ($lLangInDatabaseNow as $lItem)
	$lLangInDbArray[]=$lItem["Field"];

$lSetDefault="";
if (count($lLangSupportedArray)>0)
{
	foreach ($lLangSupportedArray as $lItem)
	{
		$lLangToSupport="opt_" . $lItem;
		if ($lSetDefault=="")
			$lSetDefault=$lItem;
		
		if (!in_array($lLangToSupport,$lLangInDbArray))
			q("ALTER TABLE `$opt_tbl` ADD $lLangToSupport VARCHAR( 255 ) $lCharSetString NULL ;");	
	}
}
$lLangSel="opt_".getParam("lang","$lSetDefault");
$lField=getParam("field","");
$lRes=q("select q_question from $q_tbl where q_field=$lField");
$lRow=mysql_fetch_array($lRes);
$lFieldQuestion=$lRow["q_question"];

if ($lFieldQuestion!=="")
	if (@constant($lFieldQuestion)!="")
		$lFieldQuestion=constant("$lFieldQuestion");
?>
<title>Admin: Extra Fields : <? echo $lFieldQuestion ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="PHP Classifieds" />
<link href="../style/style_admin.css" rel="stylesheet" type="text/css">

</head>
<body>

<form id='form1' name='form1'>
<?
echo "<input type='hidden' name='field' value='$lField' />";

if ($set_multilang_opt=="")
{
	echo "<p class='bg'><a href='adm_set.php?file=opt_inc.php#set_multilang_opt' target='_blank'>".LA_ADMIN_MULT_LANG_OPTION."</a> ".LA_ADMIN_MULT_LANG_OPTION_MORE."</p>";
	
}
else 
{
	$lArrray=$lLangSupportedArray;
	echo "<p class='bg'>".LA_ADMIN_FIELDS_CREATE_OTHER.": <select name='lang' id='lang' onchange='form1.submit()'>";

	foreach ($lArrray as $lItem)
	{
		if (!ereg("adm",$lItem))
		{
			echo "<option value='$lItem'";
			if (getParam("lang","$lSetDefault")==$lItem)
				echo " selected";
			echo ">" . $lItem . "</option>\n";
		}
		
	}
	echo "</select></p>";
}
echo "</form>";

$lLang=getParam("lang","$lSetDefault");

if (!$set_multilang_opt)
	$lLangCol="opt_name";
else 
	$lLangCol="opt_" . $lLang;


echo "<p>&nbsp;</p><table border='1' cellpadding='3' cellspacing='0' width='100%' class='blackBorderTable'><tr><td class='trHeadLine' colspan='2'><b>".LA_ADMIN_FIELDS_EXTRA_FOPTION." - $lFieldQuestion</b></td></tr><tr><td>";

if (getParam("import",""))
{
	$c=0;
	$ignored=0;
	if ($_FILES['userfile']['type']=="text/plain" && substr($_FILES['userfile']['name'],-3,3)=="txt")
	{
		$fp=fopen($_FILES['userfile']['tmp_name'],"r");
		$lFileContent=fread($fp,filesize($_FILES['userfile']['tmp_name']));
		//$lFileContent=rtrim($lFileContent);
		//$lSplitted=split("\n",rtrim($lFileContent));
		$lSplitted=preg_split("/\r?\n|\r/", $lFileContent);
		
		foreach ($lSplitted as $lItem)
		{
			$lItem=htmlspecialchars($lItem,ENT_QUOTES);
			$lCheck=q("select opt_id from $opt_tbl where $lLangCol='".trim($lItem)."' AND opt_belongto=$lField");
			if (mysql_num_rows($lCheck)>0)
			{
				$ignored++;
			}
			else 
			{
				q("insert into $opt_tbl ($lLangCol,opt_belongto) values ('".trim($lItem)."',$lField)");
				$c++;
			}
		}
	}
	echo "<tr><td class='bg'><b>$c</b> ".LA_ADMIN_FIELDS_IMP."<br />";
	
	if ($ignored>0)
	{
		echo "<font color='red'><b>$ignored</b></font> ".LA_ADMIN_FIELDS_NOT_IMP.""; 
	}
	
	echo "</td></tr>";
	
	@unlink("config/cache/getExtraForm.cache");
	@unlink("config/cache/getExtraOptionsForm.cache");
}


if (getParam("new_option",""))
{
	$lSql="insert into $opt_tbl ($lLangCol,opt_belongto) values ('".htmlspecialchars(trim(getParam("new_option","")),ENT_QUOTES)."',".$lField.")";
	$lRes=q($lSql);
	
	@unlink("config/cache/getExtraForm.cache");
	@unlink("config/cache/getExtraOptionsForm.cache");
}

if (getParam("option_name",""))
{
	$lSql="update $opt_tbl set $lLangCol='".htmlspecialchars(trim(getParam("option_name","")),ENT_QUOTES)."' WHERE opt_id=".getParam("opt_id","");
	$lRes=q($lSql);
	@unlink("config/cache/getExtraForm.cache");
	@unlink("config/cache/getExtraOptionsForm.cache");
}

if (getParam("del",""))
{
	$lSql="delete from $opt_tbl WHERE opt_id=".getParam("opt_id","") . " order by opt_id desc";
	$lRes=q($lSql);
	
	@unlink("config/cache/getExtraForm.cache");
	@unlink("config/cache/getExtraOptionsForm.cache");
}


$lRes=q("select * from $opt_tbl where opt_belongto=$lField order by opt_id desc");

echo "<tr><td>".$lRow["opt_id"]."</td><td><form name='f1' method='get' action='adm_fill_options.php'><input type='hidden' name='lang' value='$lLang'><input type='hidden' name='field' value='$lField'><input type='text' name='new_option' size='40' value=''>&nbsp;&nbsp;<input type='image' src='../layout_images/add.gif'></form></td></tr>";
while ($lRow=mysql_fetch_array($lRes))
{
	echo "<tr><td>#".$lRow["opt_id"]."</td><td><form method='get' action='adm_fill_options.php'><input type='hidden' name='lang' value='$lLang'><input type='hidden' name='opt_id' value='".$lRow["opt_id"]."'><input type='hidden' name='field' value='$lField'><input type='text' name='option_name' size='40' value='".$lRow["$lLangCol"]."'>&nbsp;&nbsp;<input type='image' src='../layout_images/yes.gif'> &nbsp; <a href='?field=$lField&opt_id=".$lRow["opt_id"]."&del=1'><img src='../layout_images/new/delete_small.gif' border='0'></a></form></td></tr>";
	
}
echo "</table>";

?>
<script language="javascript">
document.f1.new_option.focus();
</script>
<br />
<table width="100%"><tr><td class="bg">
<br /><b><? echo LA_ADMIN_FIELDS_TEXTFILE ?></b><br />
<form enctype="multipart/form-data" action="adm_fill_options.php" method="POST">
<input type='hidden' name='field' value='<? echo $lField ?>'>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<? echo LA_ADMIN_FIELDS_FILENAME ?>: <input name="userfile" type="file" /><br />
<input type="submit"  name="import" value="<? echo LA_ADMIN_FIELDS_IMPORT ?>" />
</form>
<br />
<? echo LA_ADMIN_FIELDS_IMPORT_DESCR ?>
</td></tr></table>



</body></html>